/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.interaction.keyboard.client;

import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.Designer;
import org.soupframework.gwt.designerpanel.client.designer.DesignerManager;
import org.soupframework.gwt.designerpanel.client.designer.impl.AbsolutePanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.designer.impl.CaptionPanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.designer.impl.FlowPanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.designer.impl.HorizontalPanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.designer.impl.SimplePanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.designer.impl.VerticalPanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.event.ChildDesignerAddedEvent;
import org.soupframework.gwt.designerpanel.client.event.ChildDesignerAddedEventHandler;
import org.soupframework.gwt.designerpanel.client.interaction.InteractionHandler;
import org.soupframework.gwt.designerpanel.client.interaction.impl.InteractionHandlerManagerBase;

public class KeyboardInteractionHandlerManager<T> extends InteractionHandlerManagerBase<T> {

	protected KeyboardInteractionHandlerManager(Designer<T> rootDesigner){
		super(rootDesigner);
	}

	/**
	 * Creates a {@link KeyboardInteractionHandlerManager} for the given {@link Designer}
	 *
	 * @param rootDesigner the root {@link Designer}
	 * @return the {@link KeyboardInteractionHandlerManager}
	 */
	public static <T> KeyboardInteractionHandlerManager<T> createInteractionHandlerManager(DesignerManager<T> designerManager){
		KeyboardInteractionHandlerManager<T> manager = new KeyboardInteractionHandlerManager<T>(designerManager.getRootDesigner());

		// Register event handling for root designer
		InteractionHandler<T> handler = manager.getInteractionHandler(designerManager.getRootDesigner());
		if(handler == null){
			throw new IllegalArgumentException("Cannot instantiate an interaction manager for root designer");
		}
		handler.registerHandlersOnTarget();

		return manager;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected InteractionHandler<T> createInteractionHandler(final Designer<T> designer) {
		if(designer instanceof AbsolutePanelViewDesigner){
			return new AbsolutePanelKeyboardInteractionHandler<T, AbsolutePanelViewDesigner<T>>(this, (AbsolutePanelViewDesigner<T>)designer);
		}
		else if(designer instanceof CaptionPanelViewDesigner){
			return new CaptionPanelKeyboardInteractionHandler<T, CaptionPanelViewDesigner<T>>(this, (CaptionPanelViewDesigner<T>)designer);
		}
		else if(designer instanceof FlowPanelViewDesigner){
			return new FlowPanelKeyboardInteractionHandler<T, FlowPanelViewDesigner<T>>(this, (FlowPanelViewDesigner<T>)designer);
		}
		else if(designer instanceof VerticalPanelViewDesigner){
			return new VerticalPanelKeyboardInteractionHandler<T, VerticalPanelViewDesigner<T>>(this, (VerticalPanelViewDesigner<T>)designer);
		}
		else if(designer instanceof HorizontalPanelViewDesigner){
			return new HorizontalPanelKeyboardInteractionHandler<T, HorizontalPanelViewDesigner<T>>(this, (HorizontalPanelViewDesigner<T>)designer);
		}
		else if(designer instanceof SimplePanelViewDesigner){
			return new SimplePanelKeyboardInteractionHandler<T, SimplePanelViewDesigner<T>>(this, (SimplePanelViewDesigner<T>)designer);
		}

		if(designer instanceof ContainerDesigner){
			// When new children are added add a new InteractionHandler
			((ContainerDesigner)designer).addChildDesignerAddedHandler(new ChildDesignerAddedEventHandler() {
				@Override
				public void onChildDesignerAdded(ChildDesignerAddedEvent event) {
					Designer childDesigner = event.getAddedDesigner();
					createInteractionHandler(childDesigner);
				}
			});
		}

		return null;
	}
}
